#pyinstaller --onefile 翻译word.py
import docx
from docx.shared import Pt
from datetime import datetime
import sys
import tools

if __name__=='__main__':

    s = datetime.now()
    tableCount = 0
    '''获取appid 和 appkey'''
    idkey = tools.getidkey()
    appid = idkey[0]
    appkey = idkey[1]

    files = tools.getFilesList('docx')

    if len(files)<=0:
        print('没有找到docx文件')
        input('按任意键退出...')
        sys.exit(0)

    fromLan,toLan = tools.getLanguage()

    #files = ['1']

    for file in files:
        print(f'***********正在机翻{file}***********')
        name = file.split('.')[0]
        doc = docx.Document(name+'.docx')

        #获取word总行数，总行数=行数+单元格数
        plen = len(doc.paragraphs)
        for t in doc.tables:
            row_count = len(t.rows)
            col_count = len(t.columns)
            plen += row_count * col_count

        pcount = 1
        hanzi = []
        sumHanzi = 0
        try:

            for p in doc.paragraphs:
                txt = tools.handle_text(p.text,fromLan)
                if tools.jump_text(txt):continue
                print(tools.get_process(pcount,plen)+'---'+txt)
                tran_text = tools.tran(txt, fromLan, toLan, appid, appkey)
                if toLan == 'zh': hanzi.append(tran_text + "\n");sumHanzi+=len(tran_text)
                p.add_run("\n")
                run = p.add_run(tran_text)
                run.font.size = Pt(12)  # 字号是小四
                if fromLan == 'zh':
                    run.font.name = "Times New Roman"  # 字体是新罗马
                else:
                    run.font.name = "宋体"
                pcount += 1

            jumpTable = []
            for table in doc.tables:
                tableCount += 1
                if tableCount in jumpTable:continue
                for row in table.rows:
                    for cell in row.cells:
                        text = tools.handle_text(cell.text,fromLan)
                        print(text)
                        print(tools.get_process(pcount,plen)+'---'+text)
                        tran_text = tools.tran(text, fromLan, toLan, appid, appkey)
                        if toLan == 'zh': hanzi.append(tran_text + "\n");sumHanzi+=len(tran_text)
                        p = cell.paragraphs[len(cell.paragraphs)-1]  # 假设单元格中已经有内容，获取最后个段落
                        p.add_run('\n')  # 在段落末尾追加内容
                        run = p.add_run(tran_text)
                        run.font.size = Pt(9)  # 字号是小四
                        if fromLan == 'zh':
                            run.font.name = "Times New Roman"  # 字体是新罗马
                        else:
                            run.font.name = "宋体"
                        pcount += 1



            doc.save(f'机翻-{name}.docx')

            # 如果是俄语转汉语，需要统计汉语字数
            if toLan == 'zh':
                with open(f'汉字-{name}.txt', 'w', encoding='utf-8') as resfilehanzi:
                    resfilehanzi.writelines(hanzi)
                print(f'共计:{sumHanzi}个汉字')

            print(f"完成！共计:{tools.diff_time(s,datetime.now())}分钟")
        except Exception as e :
            print(e)
            doc.save(f'机翻-{name}.docx')
            if toLan == 'zh':
                with open(f'汉字-{name}.txt', 'w', encoding='utf-8') as resfilehanzi:
                    resfilehanzi.writelines(hanzi)
                print(f'共计:{sumHanzi}个汉字')

            print(f"中断！共计:{tools.diff_time(s, datetime.now())}分钟")
    input(f"全部完成！共计:{tools.diff_time(s, datetime.now())}分钟。请按任意键关闭")